package com.gemini.main.authentication.handler;

import com.alibaba.fastjson.JSON;
import com.gemini.main.common.bean.ResultGenerator;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.security.auth.login.AccountExpiredException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Wisdytech.cn
 * com.gemini.main.authentication.handler.GeminiAuthenticationFailureHandler
 *
 * @author hailin.zhl
 * @date 2018/12/04
 */
@Component
public class GeminiAuthenticationFailureHandler implements AuthenticationFailureHandler {


    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        response.setContentType("application/json;charset=utf-8");
        String message = exception.getMessage();
        if (exception instanceof org.springframework.security.authentication.AccountExpiredException) {
            message = "账户被锁定";
        }
        response
                .getWriter()
                .write(JSON.toJSONString(ResultGenerator.genFailResult(message)));
    }
}
